/*
 * Copyright (c) 2022 Hong Jiahua
 * https://gitee.com/arrco/jh_queue
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef __JH_QUEUE_H__
#define __JH_QUEUE_H__

#include <stdlib.h>
#include <string.h>

/**
  * @brief  队列数据结构
  */
typedef struct {
    void* base;
    size_t num;
    size_t size;
    size_t head;
    size_t tail;
} jh_queue_t;

/**
  * @brief  队列数据迭代
  */
#define JH_QUEUE_ITER(queue, item)    \
    for(size_t __jh_i = (queue)->head != (queue)->tail ? memcpy(item, (queue)->base + (queue)->head * (queue)->size, (queue)->size) , (queue)->head: (queue)->head; \
        __jh_i != (queue)->tail; __jh_i = (__jh_i + 1) % (queue)->num , __jh_i = (__jh_i != (queue)->tail) ? memcpy(item, (queue)->base + __jh_i * (queue)->size, (queue)->size) , __jh_i : __jh_i)

/**
  * @brief  队列数据反向迭代
  */
#define JH_QUEUE_ITER_REVERSE(queue, item)    \
    for(size_t __jh_i = (queue)->head != (queue)->tail ? __jh_i = (queue)->tail ? (queue)->tail - 1 : (queue)->num - 1 , memcpy(item, (queue)->base + __jh_i * (queue)->size, (queue)->size) , __jh_i: (queue)->head, __jh_j = __jh_i; \
        __jh_i != (queue)->head; __jh_i = __jh_j, __jh_j = (__jh_j == (queue)->head) ? (queue)->head : (__jh_j = __jh_j ? __jh_j - 1 : (queue)->num - 1 , memcpy(item, (queue)->base + __jh_j * (queue)->size, (queue)->size), __jh_j))
    
/*判断队列是否为空*/
int jh_queue_is_empty(jh_queue_t* queue);
/*判断队列是否已满*/
int jh_queue_is_full(jh_queue_t* queue);
/*获取队列的数据数量*/
size_t jh_queue_count(jh_queue_t* queue);
/*数据入队*/
int jh_queue_push(jh_queue_t* queue, void* item);
/*数据出队*/
int jh_queue_pop(jh_queue_t* queue, void* item);
/*队尾数据出队*/
int jh_queue_pop_tail(jh_queue_t* queue, void* item);
/*查看队首的数据*/
int jh_queue_peek(jh_queue_t* queue, void* item);
/*查看队首的数据*/
int jh_queue_peek_head(jh_queue_t* queue, void* item);
/*查看队尾的数据*/
int jh_queue_peek_tail(jh_queue_t* queue, void* item);
/*清空队列*/
int jh_queue_clear(jh_queue_t* queue);
/*获取队列指定位置的指定数量数据*/
int jh_queue_get_data(jh_queue_t* queue, void* item, size_t start, size_t num);
/*队列数据排序*/
int jh_queue_sort(jh_queue_t* queue, int (*compare)(const void*, const void*));
/*队列初始化*/
int jh_queue_init(jh_queue_t* queue, void* base, size_t num, size_t size);

#endif